package com.zzf.concurrent.example.api;

import com.zzf.concurrent.example.util.TimeUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * @author GaoFeng2017
 * @date 2019/8/5 11:05
 */
@Slf4j
public class ThreadSimpleApi {
    public static void main(String[] args) {
        /* getId, getPriority */
        Thread t1 = new Thread(() -> {
                    for (int i = 0; i < 1000; i++) {
                        TimeUtil.sleep(1);
                        log.info("{}, index{}", Thread.currentThread().getName(), i);

                    }
                }, "t1");

        Thread t2 = new Thread(() -> {
            for (int i = 0; i < 1000; i++) {
                TimeUtil.sleep(1);
                log.info("{}, index{}", Thread.currentThread().getName(), i);
            }
        }, "t2");

        Thread t3 = new Thread(() -> {
            for (int i = 0; i < 1000; i++) {
                TimeUtil.sleep(1);
                log.info("{}, index{}", Thread.currentThread().getName(), i);
            }
        }, "t3");

        // id为自增的序列，++threadSeqNumber
        log.info("thread getId {},{},{},{}", Thread.currentThread().getId(), t1.getId(), t2.getId(), t3.getId());

        // 设置优先级，但不一定按优先级来执行, 特别是在有延时情况下
        t1.setPriority(Thread.MAX_PRIORITY);
        t2.setPriority(Thread.NORM_PRIORITY);
        t3.setPriority(Thread.MIN_PRIORITY);

        // 抢占式线程，交替执行
        t1.start();
        t2.start();
        t3.start();

    }
}
